# Built-In Example AI

# Title: EludeTurtle
# Author: LiJie
# Version: 1.0.1
# Date: 4/29/2021

import math
import random
import game.tcturtle

class CombatTurtle(game.tcturtle.TurtleParent):
    """Elude combat turtle.

    At the beginning of the game, the turtle randomly moves his position,
    if a direct attack is detected in the field of view, or if a wall is
    in front of him.
    """

    #-------------------------------------------------------------------------

    def class_name():
        """CombatTurtle.class_name() -> str
        Static method to return the name of the Combat Turtle AI.
        """

        return "ct_Cainiao"

    #-------------------------------------------------------------------------

    def class_desc():
        """CombatTurtle.class_desc() -> str
        Static method to return a description of the Combat Turtle AI.
        """

        return "Randomly adjust position to attack."

    #-------------------------------------------------------------------------

    def class_shape():
        """CombatTurtle.class_shape() -> (int or tuple)
        Static method to define the Combat Turtle's shape image.

        The return value can be either an integer or a tuple of tuples.

        Returning an integer index selects one of the following preset shapes:
            0 -- arrowhead (also default in case of unrecognized index)
            1 -- turtle
            2 -- plow
            3 -- triangle
            4 -- kite
            5 -- pentagon
            6 -- hexagon
            7 -- star

        A custom shape can be defined by returning a tuple of the form
        (radius, angle), where radius is a tuple of radii and angle is a tuple
        of angles (in radians) describing the polar coordinates of a polygon's
        vertices. The shape coordinates should be given for a turtle facing
        east.
        """

        """
        代码待修改 return 8
        """
        return 1

    #=========================================================================

    def setup(self):
        """CombatTurtle.setup() -> None
        Initialization code for Combat Turtle.
        """
        pass

    #-------------------------------------------------------------------------


    def step(self):
        """CombatTurtle.setup() -> None
        Step event code for Combat Turtle.
        """

        self.turn_towards()

        # Shoot if facing opponent, there is line of sight, and close enough
        if (self.can_shoot and abs(self.relative_heading_towards()) <= 5 and
                self.line_of_sight() and self.distance() <= self.missile_range):
            self.shoot()
        # Determine behavior based on whether there is line of sight


    def _move(self):
        """TurtleParent._move() -> None
        Moves a turtle according to its speed attribute.

        User visibility:
            should call -- no
            should overwrite -- no

        This is a hidden method called during the step event to handle any
        speed changes that the submodule has enacted using the visible
        forward() and backward() methods (or their aliases).
        """

        self.rangeX = [self.arena_left, self.arena_right]
        self.rangeY = [self.arena_bottom, self.arena_top]

        # Set new coordinates
        if self._shooting == False:
            self._x = int(random.randrange(*self.rangeX))
            self._y = int(random.randrange(*self.rangeY))
        else:
            self._x += int(self.speed * math.cos(math.radians(self.heading)))
            self._y -= int(self.speed * math.sin(math.radians(self.heading)))

        # Check whether the destination intersects any blocks
        blocks = self._game.intersections((self.x, self.y))
        if len(blocks) > 0:
            # If so, check all intersecting blocks and move to outside
            for b in blocks:
                # Determine overlap on each side
                overlap = [1000000 for i in range(4)]  # ordered overlaps
                if self.x >= b.left:
                    overlap[0] = self.x - b.left
                if self.x <= b.right:
                    overlap[1] = b.right - self.x
                if self.y >= b.bottom:
                    overlap[2] = self.y - b.bottom
                if self.y <= b.top:
                    overlap[3] = b.top - self.y

                # Find minimum nonzero overlap
                mo = overlap.index(min(overlap))

                # Reset coordinates based on minimum overlap
                if mo == 0:
                    self._x -= overlap[0] - 1
                elif mo == 1:
                    self._x += overlap[1] + 1
                elif mo == 2:
                    self._y -= overlap[2] - 1
                else:
                    self._y += overlap[3] + 1